package com.itljm.cxrMarket.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itljm.cxrMarket.entity.Products;
import com.itljm.cxrMarket.service.ProductsService;
import com.itljm.cxrMarket.mapper.ProductsMapper;
import com.itljm.cxrMarket.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductsServiceImpl extends ServiceImpl<ProductsMapper, Products> implements ProductsService{
    @Autowired
    private ProductsMapper productsMapper;

    @Override
    public List<Products> selectProductPage(Page<Products> page, Wrapper<Products> entityWrapper) {
        return productsMapper.selectProductPage(page, entityWrapper);
    }

    @Override
    public boolean saveProduct(Products entity) {
        return productsMapper.insertOrUpdate(entity);
    }

    @Override
    public boolean deleteProductById(Integer id) {
        return productsMapper.deleteById(id)>0;
    }

    @Override
    public List<Products> selectProductByCategoryId(Integer categoryId) {
        if(categoryId==null){
            return null;
        }
        QueryWrapper<Products> wrapper = new QueryWrapper<>();
        wrapper.eq("category_id", categoryId);

        return productsMapper.selectList(wrapper);
    }

    @Override
    public Products selectProductById(Integer id) {
        if(StringUtils.isEmpty(id.toString())){
            return null;
        }else {
            return productsMapper.selectOne(new QueryWrapper<Products>().eq("product_id", id));
        }
    }

    @Override
    public boolean updateProductStatusByMerchantId(Integer merchantId, Integer status) {
        return productsMapper.updateProductStatusByMerchantId(merchantId, status)>0;
    }
}




